const bodyParser = require('body-parser')
const Mock = require('mockjs')
const userList = [{
	username: 'admin',
	password: 'admin'
}, {
	username: '123',
	password: '123'
}]
const data = Mock.mock({
	'list|50': [{
		'title': '@ctitle',
		'desc': "@cword(10,20)",
		'url': '@image(20x20, @color)',
		'type|1': ['数码', '电视', '电脑', '手机'],
		'type1|1': ['哈哈', '呵呵', '嘻嘻', '噗嗤'],
		'key': '@id',
		'id': '@id'
	}]
})

module.exports = app => {
	app.use(bodyParser.json())
	// 数据请求接口
	app.get('/api/table/list', (req, res) => {
		res.send({
			code: 200,
			data: data.list
		})
	})

	// 登录
	app.post('/api/user/login', (req, res) => {
		const { username, password } = req.body;
		const currUser = userList.find(v => v.username === username && password === v.password)
		if (currUser) {
			res.send({
				code: 200,
				mess: '登录成功， 跳转首页谁',
				token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxs'
			})
		} else {
			res.send({
				code: 403,
				mess: '登录失败， 账户密码错误'
			})
		}
	})
}